<?php
      $command = "RELOAD";
      if ($port==0) $port = "27500";
      $host = "127.0.0.1";

      echo "<li>Sending Command to Perl backend...";
      $host = gethostbyname($host);
      $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
      $packet = "C;".$command.";";
      $bytes_sent = socket_sendto($socket, $packet, strlen($packet), 0, $host, $port);
      echo "<b>".$bytes_sent."</b> bytes <b>OK</b></li>";

      echo "<li>Waiting for Backend Answer...";
      $recv_bytes = 0;
      $buffer     = "";
      $timeout    = 5;
      $answer     = "";
      $packets    = 0;
      $read       = array($socket);
      while (socket_select($read, $write = NULL, $except = NULL, &$timeout) > 0) {
        $recv_bytes += socket_recvfrom($socket, &$buffer, 2000, 0, &$host, &$port);
        $answer     .= $buffer;
        $buffer     = "";
        $timeout    = "1";
        $packets++;
      }
      echo "recieving <b>$recv_bytes</b> bytes in <b>$packets</b> packets...<b>OK</b></li>";

      if ($packets>0) {
       echo "<li>Backend Answer: ".$answer;
      } else {
       echo "<li><i>No packets received - check if backend dead or not listening on $host:$port</i>";
      }

      echo "<li>Closing connection to backend...";
      socket_close($socket);
      echo "<b>OK</b></li>";
      echo "</ul>\n";
?>
